package com.tjsinfo.tjsoa.workTask.action;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.workTask.bo.ITDailyWorkTaskBO;
import com.tjsinfo.tjsoa.workTask.vo.TDailyWorkTask;

public class WorkTaskAddAction extends Action {
	private ITDailyWorkTaskBO workTaskBo;
	
	public void setWorkTaskBo(ITDailyWorkTaskBO workTaskBo) {
		this.workTaskBo = workTaskBo;
	}
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		try {		
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			
			String id = request.getParameter("id");           //转发
			String subject = request.getParameter("subject");
			String place = request.getParameter("place");
			Date beginTime = sdf.parse(request.getParameter("beginTime"));
			Date endTime = sdf.parse(request.getParameter("endTime"));
			String dividerTask = request.getParameter("dividerTask");
			Integer importLevel = Integer.parseInt(request.getParameter("importLevel"));
			Integer dividerUserid = ToolKit.getSessionUser(request).getUser_id();
			String dividerUsername = ToolKit.getSessionUser(request).getUsername();
			String dividerDeptid = ToolKit.getSessionUser(request).getUUID();
			if (dividerDeptid == null) {
				dividerDeptid = "1";
			}
			//String dividerDeptname = ToolKit.getSessionUser(request).getUsername();
			String recipientUserid[] = request.getParameter("recipientUserid").split(",");
			String recipientUsername[] = request.getParameter("recipientUsername").split(",");
			String recipientDeptid[] = request.getParameter("recipientDeptid").split(",");
			//String recipientDeptname[] = request.getParameter("recipientDeptname").split(",");
			
			
			TDailyWorkTask tdwt = new TDailyWorkTask();
			tdwt.setSubject(subject);
			tdwt.setPlace(place);
			tdwt.setBeginTime(beginTime);
			tdwt.setEndTime(endTime);
			tdwt.setDividerTask(dividerTask);
			tdwt.setImportLevel(importLevel);
			tdwt.setDividerUserid(dividerUserid.toString());
			tdwt.setDividerUsername(dividerUsername);
			tdwt.setDividerDeptid(dividerDeptid);
			tdwt.setCurrentStatus(0);
			if(id!=null){
				tdwt.setTurn(id);
			}
			
			//TSystemDepartment td=(TSystemDepartment) qbcDao.getObjectById(TSystemDepartment.class, ToolKit.getSessionUser(request).get);
			//tdwt.setDividerDeptname(dividerDeptname);
			
			for (int i = 0; i < recipientUserid.length; i++) {
				tdwt.setRecipientUserid(recipientUserid[i]);
				tdwt.setRecipientUsername(recipientUsername[i]);
				tdwt.setRecipientDeptid(recipientDeptid[i]);
				//tdwt.setRecipientDeptname(recipientDeptname[i]);
				//workTaskBo.addWorkTask(tdwt);
				workTaskBo.save(tdwt);
			}
			
			request.setAttribute("linkurl", "daily/workTask/workTaskList.jsp?dividerUserid=dividerUserid");
			request.setAttribute("message", "system.save.message");
			return mapping.findForward("success");
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			request.setAttribute("Exception", e);
			return mapping.findForward("failed");
		}
	}
}
